package com.timu.bill.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.timu.bean.RequestMsg;
import com.timu.bean.ServiceResult;
import com.timu.bean.Trans2002Req;
import com.timu.bean.Trans2002Resp;
import com.timu.bill.service.AdjBillDayService;
import com.timu.common.AbstractEsbServiceRequest;
import com.timu.common.EaipServiceMeta;
import com.timu.common.EaipServiceParam;
import com.timu.core.ServiceInvoker;
import com.timu.core.ServiceResultParser;
import com.timu.utils.MessagePacker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @描述:
 * @Author: zhangh
 * @Time: 2021/1/26 11:35
 **/
@Service
public class AdjBillDayServiceImpl implements AdjBillDayService {
    @Autowired
    private AbstractEsbServiceRequest unionPayForwardEsbServiceRequest;
    @Autowired
    private ServiceInvoker eaipServiceInvoker;
    @Autowired
    private ServiceResultParser unionPayServiceResultParser;

    @Override
    public ServiceResult adjBillDay(RequestMsg webhookServer, EaipServiceMeta eaipServiceMeta) {
        EaipServiceParam eaipServiceParam = new EaipServiceParam();
        eaipServiceParam.setSessionId(webhookServer.getSession_id());
        eaipServiceParam.setQueryId(webhookServer.getQuery_id());
        Object context = webhookServer.getContext();
        if (context!=null){
            String jsonString = JSON.toJSONString(context);
            Map<String,Object> map = JSONObject.parseObject(jsonString, Map.class);
            Object cardId = map.get("cardId");
            Object stmt_day = map.get("stmt_day");
            Object opt_flag = map.get("opt_flag");
            Object cancl_flag = map.get("cancl_flag");

            /**
             * .....参数校验，封装公共参数
             */

            //封装请求对象
            Trans2002Req trans2002Req = new Trans2002Req();
            trans2002Req.setCancl_flag(cancl_flag.toString());
            trans2002Req.setCardId(cardId.toString());
            trans2002Req.setOpt_flag(opt_flag.toString());
            trans2002Req.setStmt_day(stmt_day.toString());

            //使用兴业自定义包将请求对象封装成定长报文
           byte[] bytedata= (byte[]) MessagePacker.pack("trans2002Req",trans2002Req);
           String data=new String(bytedata);
           eaipServiceParam.setMessage(unionPayForwardEsbServiceRequest.requestParam(data));

        }

        return eaipServiceInvoker.doService(eaipServiceMeta,eaipServiceParam,unionPayServiceResultParser);





    }
}
